package com.neu.wangweili.dao;

import com.neu.wangweili.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;

public class BatchBaseDao {
    protected Connection conn=null;
    protected PreparedStatement stmt=null;
    protected ResultSet rst=null;
    public Connection getConn() throws SQLException {
        conn=DBUtil.getConnection();
        return conn;
    }
    public void close(){
        DBUtil.close(conn,stmt,rst);
    }
//    此处的stuBatchDelete方法名应为stuBatchUpdate,用于只有一条信息修改的update操作
    public int[] stuBatchDelete(String sql,String []stu_id){
        int[] rows=null;
        try {

            getConn();
            stmt=conn.prepareStatement(sql);
            System.out.println();
            for (int i = 0; i < stu_id.length; i++) {
//                stmt.setObject(1,stu_statis);中的1,2,3为第几个问号
                stmt.setObject(1,stu_id[i]);
                stmt.addBatch();
            }
            rows=stmt.executeBatch();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close();
        }
        return rows;
    }
    public int[] stuBatchAppiont(String sql,String stu_statis,String []stu_id){
        int[] rows=null;
        try {
            getConn();
            stmt=conn.prepareStatement(sql);
            System.out.println();
            for (int i = 0; i < stu_id.length; i++) {
//                stmt.setObject(1,stu_statis);中的1,2,3为第几个问号
                stmt.setObject(1,stu_statis);
                stmt.setObject(2,stu_id[i]);
                stmt.addBatch();
            }
            rows=stmt.executeBatch();
            System.out.println(Arrays.toString(rows));

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close();
        }
        return rows;
    }
}
